package com.ssy.wireless.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssy.wireless.entity.Hotspot;
import com.ssy.wireless.entity.MessageAuth;
import com.ssy.wireless.entity.MessageAuthStat;
import com.ssy.wireless.util.DateUtils;
import com.ssy.wireless.util.RespVo;
import com.ssy.wireless.web.service.HotspotService;
import com.ssy.wireless.web.service.MessageAuthService;
import com.ssy.wireless.web.vo.req.MessageAuthQueryVo;
import com.ssy.wireless.web.vo.resp.EchartsBarVo;
import com.ssy.wireless.web.vo.resp.EchartsSeriesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * Created by cofey on 16/11/20.
 */
@RestController
@RequestMapping("/ma")
public class MessageAuthController extends AbstractController {
    @Autowired
    private MessageAuthService messageAuthService;
    @Autowired
    private HotspotService hotspotService;

    @GetMapping("/")
    public ModelAndView list() {
        return new ModelAndView("ma/list");
    }

    @PostMapping("/content")
    public ModelAndView content(
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
            MessageAuthQueryVo vo
    ) {
        Assert.isTrue(pageSize < 200, "每页显示条数不能大于200");
        Page<MessageAuth> page = PageHelper.offsetPage(pageIndex - 1, pageSize);
        messageAuthService.find(vo.queryParams());
        return new ModelAndView("ma/content", result(page));
    }

    @GetMapping("/statistics")
    public ModelAndView statistics() {
        MessageAuthStat stat = messageAuthService.selectLastYearhStat();
        List<Hotspot> hotspots = null;//TODO 暂时不要 hotspotService.find(null);
        Map<String, Object> result = new HashMap<>();
        result.put("hotspots", hotspots);
        result.put("stat",stat);
        return new ModelAndView("ma/statistics", result);
    }

    @PostMapping("/statistics/content")
    public RespVo<EchartsBarVo> statisticsContent(@RequestParam(value = "type", defaultValue = "1", required = false) int type,
                                                  @RequestParam(value = "hotspotId", required = false) String hotspotId,
                                                  @RequestParam(value = "beginDate", required = false) String beginDate,
                                                  @RequestParam(value = "endDate", required = false) String endDate) {
        List<String> legends = new ArrayList();
        legends.add("请求数量");
        legends.add("发送数量");
        legends.add("登录数量");
        xAxisAndBeginAndEndDate abe = setxAxisAndBeginAndEndDate(type, beginDate, endDate);
        List<String> xAxis = abe.getxAxis();
        List<EchartsSeriesVo> series = new ArrayList();
        Map<String, Object> params = new HashMap();
        params.put("type", type);
        params.put("hotspotId", StringUtils.hasLength(hotspotId) ? Integer.valueOf(hotspotId) : null);
        params.put("begin", abe.getBegin());
        params.put("end", abe.getEnd());
        List<MessageAuthStat> statistics = messageAuthService.findStatistics(params);
        for (int i = 0; i < legends.size(); i++) {
            EchartsSeriesVo vo = new EchartsSeriesVo();
            vo.setName(legends.get(i));
            List<Integer> data = new ArrayList();
            for (String date : xAxis) {
                data.add(setDataItem(type, legends.get(i), date, statistics));
            }
            vo.setData(data);
            series.add(vo);
        }
        return RespVo.success(EchartsBarVo.build().createTitle("短信认证统计").createLegend(legends).createxAxis(xAxis).createSeries(series));
    }

    private Integer setDataItem(int type, String legend, String date, List<MessageAuthStat> stats) {
        int val = 0;
        for (MessageAuthStat mas : stats) {
            String dateVal = date.replaceAll("-", "");
            switch (type) {
                case 1:
                    dateVal = DateUtils.format(DateUtils.getTime(), "yyyyMMdd") + date;
                    val = mas.getHourVal().toString().equals(dateVal) ? getVal(legend, mas) : 0;
                    break;
                case 2:
                    val = mas.getDayVal().toString().equals(dateVal) ? getVal(legend, mas) : 0;
                    break;
                case 3:

                    break;
                case 4:
                    val = mas.getMonthVal().toString().equals(dateVal) ? getVal(legend, mas) : 0;
                    break;
                default:
                    val = mas.getYearVal().toString().equals(date) ? getVal(legend, mas) : 0;
                    break;
            }
            if (val > 0) break;
        }
        return val;
    }

    private Integer getVal(String legend, MessageAuthStat mas) {
        if (legend.equals("请求数量")) {
            return mas.getRequestCount();
        } else if (legend.equals("发送数量")) {
            return mas.getSendCount();
        } else {
            return mas.getLoginCount();
        }
    }
}
